const service = require('../../request').service;
const model = require("../../../utils/modelUtil");
const md5 = require('md5');

/**
 * 娃娃机
 */
class UFOCatcher {
    constructor(user) {
        this.user = user;
    }

    /**
     * 获取当前数据
     * @returns {Promise<unknown>}
     */
    getCurrentData() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/getUFOCatcherData',
                data: model.userModel('getUFOCatcherData', {}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 获取奖励
     * @param packages 包数组
     * @param itemId 道具ID
     * @param signId
     * @returns {Promise<unknown>}
     */
    getReward(packages, itemId, signId) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/rewardUFO',
                data: model.userModel('rewardUFO', {packages: packages, itemId: itemId, signCode: md5((2*signId+37).toString())}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }


    freeRefresh() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/useUFOrefresh',
                data: model.userModel('useUFOrefresh', {}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = UFOCatcher;